package com.bdqn.ch07.service.impl;

import com.bdqn.ch07.mapper.SysUserMapper;
import com.bdqn.ch07.pojo.Supplier;
import com.bdqn.ch07.pojo.SysUser;
import com.bdqn.ch07.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * SysUserServiceImpl
 *
 * @aurhor Administrator  whs
 * @since 2024/7/19
 */

@Transactional // 为该类的所有业务方法统一添加事务处理
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Autowired  //@ Resource
    private SysUserMapper sysUserMapper;

    @Transactional(propagation = Propagation.SUPPORTS)

    /**-
     * 查询列表
     * @param sysUser
     * @return
     */
    @Override
    public List<SysUser> getList(SysUser sysUser) {
        try {


           return sysUserMapper.selectSysUserList(sysUser);
//            return sysUserMapper.selectSysUserList(sysUser);
        }catch (RuntimeException e){

            e.printStackTrace();
            throw e;
        }

    }

    /**
     * 判断是否保存成功
     *
     * @param sysUser
     * @return
     */
    @Override
    public boolean add(SysUser sysUser) {
        boolean flag = false;
        try {
            if (sysUserMapper.add(sysUser) == 1){
                flag = true;
                // 测试事务回滚，打开注释
                // throw new RuntimeException()；
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return flag;
    }

    /**
     * 查询全部供货商
     *
     * @return
     */
    @Override
    public List<Supplier> selectAllSupplier() {
        return sysUserMapper.selectAllSupplier();
    }

    /**
     * ch07 实战4
     * 根据供货商ID进行删除
     *
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public int deleteIdSupplier(int id) {
        return sysUserMapper.deleteIdSupplier(id);
    }

    /**
     * ch07 实战4
     * 根据供货商id 修改供货商信息
     *
     * @param supplier
     * @return
     */
    @Override
    public int updateSupplierId(Supplier supplier) {
        return sysUserMapper.updateSupplierId(supplier);
    }


    public SysUserMapper getSysUserMapper() {
        return sysUserMapper;
    }

    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }
}
